<?php

namespace App\Http\Controllers;

use App\Models\LawyerModel;
use App\Services\LawyerService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LawyerController extends BaseController {

    public function index(Request $request) {
        return view('lawyer.index', $this->view_data);
    }

    public function list(Request $request) {
        $params = $request->all();
        $list = (new LawyerService())->list($params);
        return $this->apiSuccess($list);
    }

    public function create(Request $request) {
        $info_id = $request->get("info_id",0);
        $info = [];
        if (!empty($info_id)) {
            $info = (new LawyerService())->info($info_id);
        }
        $this->view_data['info'] = $info;
        return view('lawyer.create', $this->view_data);
    }

    public function save(Request $request) {
        $data = $request->all();
        $roules = [
            'name' => 'required|max:150',
            'type' => 'required',
            'address' => 'required',
            'phone' => 'required',
            'email' => 'required|max:100',
            'field' => 'required',
            'work_exp' => 'required',
            'honor' => 'required',
            'images' => 'required',
        ];
        $message = [
            'name.required' => '请填写律师姓名',
            'name.max' => '律师姓名长度不能超过100个字',
            'type.required' => '请选择律师类型',
            'address.required' => '请填写联系地址',
            'phone.required' => '请填写联系电话',
            'email.required' => '请填写Email',
            'field.required' => '请填写擅长领域',
            'work_exp.required' => '请填写工作经历',
            'honor.required' => '请填写个人荣誉',
            'images.required' => '请上传至少一张图片',
        ];
        $validator = Validator::make($request->post(), $roules, $message);
        if ($validator->fails()) {
            $error_msg = $validator->errors()->first();
            return $this->apiError($error_msg, 1);
        }

        $info_id = $request->post('info_id', 0);
        if (!empty($info_id)) {
            $model = LawyerModel::find($info_id);
        }else{
            $model = new LawyerModel();
        }
        $model->name = data_get($data, 'name', '');
        $model->type = data_get($data, 'type', '律师');
        $model->address = data_get($data, 'address', '');
        $model->phone = data_get($data, 'phone', '');
        $model->email = data_get($data, 'email', '');
        $model->field = data_get($data, 'field', '');
        $model->work_exp = data_get($data,'work_exp', '');
        $model->honor = data_get($data, 'honor', '');
        $model->images = data_get($data, 'images', '');
        $model->save();

        if (!$model->id) {
            return $this->apiError('保存失败');
        }
        return $this->apiSuccess('保存成功');
   }

   public function delete(Request $request) {
        $info_id = $request->get('info_id', 0);
        if (empty($info_id) || !is_numeric($info_id)) {
            return $this->apiError('请选择要删除的律师');
        }
       (new LawyerService())->delete($info_id);
        return $this->apiSuccess('删除成功！');
   }
}